package com.note.servlet.note;

import com.note.pojo.Notetype;
import com.note.pojo.page;
import com.note.service.note.noteService;
import com.note.service.note.noteServiceImpl;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class noteShow extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");
        int pageSize = 5;
        noteService noteService = new noteServiceImpl();
        page page = noteService.allNote(1, pageSize);
        if (currentPage==null || currentPage.equals("0")){
            currentPage = "1";
        }else if(Integer.parseInt(currentPage) == page.getTotalPage()+1){
            currentPage = String.valueOf(page.getTotalPage());
        }

        page = noteService.allNote(Integer.parseInt(currentPage), pageSize);
        List<Notetype> typeList = noteService.getTypeList();
        req.setAttribute("page", page);
        req.setAttribute("typeList", typeList);
        req.getRequestDispatcher("jsp/noteManage.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
